package com.woniumall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniumall.dao.GoodsDao;
import com.woniumall.pojo.Goods;
import com.woniumall.pojo.PageBean;
import com.woniumall.pojo.Remark;
import com.woniumall.service.GoodsService;
import com.woniumall.util.sqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

import java.text.DateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * @auther linke
 * @create 2021-10-28 16:25
 * 书籍服务层的具体实现类
 */
public class GoodsServiceImpl implements GoodsService {
private  SqlSession session = null;
private  GoodsDao goodsDao = null;
    /**
     * 访问持久层获得新书参数并返回
     * @return
     * @throws Exception
     */
    @Override
    public List<Goods> findNewGoods() {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
        List<Goods> list =goodsDao.findNewGoods();
        //System.out.println(list);

        return list;
    }
    public List<Goods> findHotGoods() {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
        List<Goods> list = goodsDao.findHotGoods();
          System.out.println(list);


        return list;
    }

    @Override
    public Goods findById(int id) {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
       Goods goods = goodsDao.findById(id);
        session.close();//释放资源
        return goods;
    }

    @Override
    public PageBean<Goods> findPageByCid(int cid, int currentPage) {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
        PageBean<Goods> pageBean =  new <Goods>PageBean();
        //设置当前页码
        pageBean.setCurrentPage(currentPage);
        //设置每页显示的商品数
        pageBean.setPageSize(6);
        //设置总商品数
        int count = goodsDao.countByCid(cid);
        pageBean.setTotalCount(count);
        //设置总页数
        pageBean.setTotalPage(count%6 == 0 ? count/6 : count/6+1);
        //设置每页展示的商品对象
        List<Goods> list = goodsDao.findPageByCid(cid,(currentPage-1)*6);
        pageBean.setList(list);
        session.close();//释放资源
        return pageBean;
    }

    @Override
    public List<Goods> findBysalenums( int num) {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
        List<Goods> list = goodsDao.findBysalenums(num);
        session.close();//释放资源
        return list;
    }
    //查询更多新书
    @Override
    public PageInfo<Goods> findNewGoodsPage(int currentPage) {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
        //设置起始索引的显示个数
        PageHelper.startPage(currentPage,6);
        List<Goods> list = goodsDao.findByNewGoodsPage();
        PageInfo<Goods> lists = new PageInfo<>(list);
        System.out.println(lists);
        session.close();//释放资源
        return lists;
    }
    //查询更多热门图书
    @Override
    public PageInfo<Goods> findHotGoodsPage(int currentPage) {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
        //设置起始索引的显示个数
        PageHelper.startPage(currentPage,6);
        List<Goods> list = goodsDao.findByHotGoodsPage();
        PageInfo<Goods> lists = new PageInfo<>(list);
        System.out.println(lists);
        session.close();//释放资源
        return lists;
    }

    @Override
    public PageInfo<Goods> search(String keyword, int currentPage) {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
        //设置起始索引的显示个数
        PageHelper.startPage(currentPage,6);
        List<Goods> list = goodsDao.search(keyword);
        PageInfo<Goods> lists = new PageInfo<>(list);
        System.out.println(lists);
        session.close();//释放资源
        return lists;
    }

    @Override
    public PageInfo<Remark> showComment(int goodsid, int currentPage) {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
        //设置起始索引的显示个数
        PageHelper.startPage(currentPage,6);
        List<Remark> list = goodsDao.showComment(goodsid);
        PageInfo<Remark> lists = new PageInfo<>(list);
        System.out.println(lists);
        session.close();//释放资源
        return lists;
    }

    @Override
    public int addComment(int goodsid, String comment, int userid) {
        session = sqlSessionUtil.sqlSession();
        goodsDao = session.getMapper(GoodsDao.class);
        //获取时间
        DateFormat df3 = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.CHINA);
        DateFormat df7 = DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.CHINA);
        String day = df3.format(new Date());
        String hour = df7.format(new Date());

        Remark remark = new Remark();
        remark.setGoodsid(goodsid);
        remark.setContent(comment);
        remark.setUserid(userid);
        remark.setScore(5);//默认5星好评   懒得写 主要是人懒
       remark.setRemarktime(day+hour);
       remark.setStatus("1");
        int row = goodsDao.addComment(remark);
        if(row < 1){
            session.close();//释放资源
            return 0;
        }else {
            session.commit();
            session.close();//释放资源
            return row;
        }
    }


}
